package com.lobo.blog.controller;

import com.github.pagehelper.PageInfo;
import com.lobo.blog.entity.Blog;
import com.lobo.blog.entity.Type;
import com.lobo.blog.service.BlogService;
import com.lobo.blog.service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * @authoer:MacieSerenity
 * @createDate: 2022/10/15
 * @description:
 */
@Controller
public class TypeContorller {

    @Resource
    TypeService typeService;
    @Resource
    BlogService blogService;

    @RequestMapping(value = "/types/{id}", method = RequestMethod.GET)
    public String toTypesInfo(@PathVariable Integer id,
                              Integer page,
                              Model model) {
        System.err.println(page);
        List<Type> topTypeBySize = typeService.getTopTypeBySize(100);
        if (id==-1) id=topTypeBySize.get(0).getId();
        Blog blog=new Blog();
        blog.setTypeId(id);
        model.addAttribute("types",topTypeBySize);
        if (page==null){
            page=1;
        }
        PageInfo<Blog> blogsByCondition = blogService.getBlogsByCondition(page, blog);
        model.addAttribute("page",blogsByCondition);
        model.addAttribute("activeTypeId",id);
        return "types";
    }


}
